<template>
  <div>
    <div v-loading="loading" class="text item">
      <PoemBody v-for="(item,index) in poemList" :key="index" :titleUrl="true" :poemData="item"/>
    </div>
    <el-pagination
        :page-size="10"
        :pager-count="11"
        @current-change="currentChange"
        layout="prev, pager, next"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
import PoemBody from '@/components/PoemBody'
import {reqGetPoemsAll} from '@/API/modules/poem'
import {reqGetAuthorPoems} from '@/API/modules/author'

export default {
  name: "PoemList",
  components: {PoemBody},
  data() {
    return {
      poemList: [],
      total: 10,
      loading: true
    }
  },
  methods: {
    //获取全部诗词
    async getAllPoems(currentPage = 1) {
      let result = await reqGetPoemsAll(currentPage)
      if (result.status == 200) {
        this.poemList = result.data.data.poemsAndAuthorsList
        this.total = result.data.data.total
        this.loading = false
      }
    },
    //获取诗人的所有诗词
    async getAuthorPoems(aid, currentPage = 1) {
      let result = await reqGetAuthorPoems(aid, currentPage)
      if (result.status == 200) {
        this.poemList = result.data.data.records
        this.total = result.data.data.total
        this.loading = false
      }
    },
    currentChange(val) {
      if (this.$route.params.aid) {
        this.getAuthorPoems(this.$route.params.aid, val)
      } else {
        this.getAllPoems(val)
      }
    }
  },
  watch: {
    $route() {
      if (!this.$route.params.aid) this.getAllPoems()
    }
  },
  created() {
    if (this.$route.params.aid) {
      this.$store.commit('setBread', {toBreadName: `${this.$route.params.aname}的诗词`})
      this.getAuthorPoems(this.$route.params.aid)
    } else {
      this.getAllPoems()
    }
  }

}
</script>

<style scoped>

</style>